<template>
  <basic-dialog
    fullscreen
    :visible.sync="recordVisible"
    :title="'出入库记录'"
    :show-footer="false"
    :handleClose="handleCloseDialog"
    :handleOpen="handleOpenDialog"
  >
    <BasicTable
      showPagination
      :showSelectCol="false"
      :dataSource="tableInfo.list"
      :columns="tableInfo.columns"
      :loading="tableInfo.loading"
      :tableHeight="600"
      :pagination="pagination"
      :pageSizes="pageSizes"
      :total="pagination.totalNum"
      @pageChange="handlePageChange"
      @pageSizeSelect="handlePageSizeChange"
    >
      <div slot="content_context">
        <el-button
          v-if="showBtn('导出')"
          type="primary"
          size="mini"
          icon="el-icon-download"
          :loading="exportLoading"
          :disabled="!tableInfo.list.length"
          @click="exportPriceLog"
        >导出</el-button
        >
      </div>
    </BasicTable>
  </basic-dialog>
</template>
<script>
import BasicDialog from '@/components/BasicDialog';
import BasicTable from '@/components/skusTable';
import basePage from '@/mixins/commodity/basePage';
import { initRecordColumns } from '../constants';
import { createExcelByExport } from '@/utils/bizUtils';
import {
  stockRecordService,
  stockRecordExportService,
  queryStockRecordDetailService
} from '@api/stock/stockOverflow';

export default {
  name: 'RecordDialog',
  mixins: [basePage],
  components: {
    BasicDialog,
    BasicTable,
  },
  props: {
    recordVisible: {
      type: Boolean,
      default: false
    },
    selectIds: {
      type: Array,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      detailLoading: false,
      queryData: [],
      tableInfo: {
        list: [],
        columns: initRecordColumns(this),
        loading: false
      },
      pagination: {
        nowPage: 1,
        totalNum: 0,
        pageShow: 100
      },
      pageSizes: [100, 1000]
    };
  },
  methods: {
    handleOpenDialog () {
      this.queryData = this.selectIds;
      this.getTableData()
    },
    // 关闭弹框
    handleCloseDialog () {
      this.$emit('update:recordVisible', false);
    },
    // 获取表格数据
    async getTableData () {
      const param = {
        ...this.pagination,
        'idList': this.queryData
      };
      const { retData } = await stockRecordService(
        param,
        state => { this.tableInfo.loading = state }
      );
      if (retData) {
        this.tableInfo.list = retData.results;
        this.pagination.totalNum = retData.totalNum;
      }
    },
    // 获取记录详情
    async queryStockRecordDetail (row, index) {
      const { relRecordNo, orderType, skuNo, detailList } = row
      if (detailList) {
        return
      }
      const { retData } = await queryStockRecordDetailService(
        { relRecordNo, orderType, skuNo },
        state => { this.detailLoading = state }
      );
      if (retData) {
        this.$set(this.tableInfo.list, index, { ...row, detailList: retData })
      }
    },
    // 导出信息数据
    async exportPriceLog () {
      const res = await stockRecordExportService(
        this.queryData,
        state => { this.exportLoading = state; }
      );
      createExcelByExport({ res, excelTitle: '出入库记录' });
    },
    // 页码切换
    handlePageChange (val) {
      this.pagination.nowPage = val;
      this.getTableData();
    },
    // 每页条数切换
    handlePageSizeChange (val) {
      this.pagination.pageShow = val;
      this.getTableData();
    },
  }
};
</script>
